<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%
String parAnio = request.getParameter("anio");
String parCodBaan = request.getParameter("codBaan");
String parCodBaanCompleto = request.getParameter("codBaanCompleto");
String parDescripcion = request.getParameter("descripcion");
String parNombre = request.getParameter("nombre");
String parTipoGrano = request.getParameter("tipoGCod");

String parModalidad = request.getParameter("modalidad");
String parId_lote = request.getParameter("id_lote");
String parAnioAnt = request.getParameter("anioAnt");
String parCodBaanAnt = request.getParameter("codBaanAnt");

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
ConnRs.setAutoCommit(false);
String query ="";
PreparedStatement StatementRs =null;
Statement updateStatement = null;
ResultSet Rsquery = null;
boolean Rsquery_hasData = false;
String error = null;
String insertarDetalleLote = null;
String borrarDetalleLote = null;
try{
	if (parModalidad!=null && parModalidad.equals("nuevo")) {
		query="SELECT anio, codigo_baan FROM lote WHERE anio = "+parAnio+" AND codigo_baan = '"+parCodBaan+"'";
		StatementRs=ConnRs.prepareStatement(query);
		Rsquery=StatementRs.executeQuery();
		Rsquery_hasData = Rsquery.next();
		if(Rsquery_hasData){
			out.print("<script>");
			out.print("alert('El Lote que desea insertar ya existe, por favor, inserte un Lote distinto');");
			out.print("history.go(-1);");
			out.print("</script>");
		}else{
			//--- agrega el lote a la tabla lote
			query="INSERT INTO lote (anio,codigo_baan,descripcion,nombre,tipo_grano) VALUES "+
					"("+parAnio+",'"+parCodBaan+"','"+parDescripcion+"','"+parNombre+"',"+parTipoGrano+")";
			updateStatement = ConnRs.createStatement();
			updateStatement.addBatch(query);
			//Agregar detalle lote
			insertarDetalleLote = 
				"insert 	into Lote_Detalle ( "+
				"	lote,"+
				"	planta,"+
				"	linea,"+
				"	numerooc,"+
				"	numeroio) "+
				" select (select max(id_lote) from lote), "+
				"	Planta, "+
				"	Linea, "+
				"	NumeroOC,"+ 
				"	NumeroIO "+
				" from 	OCSap"+
				" where 	NumeroOC = '"+parCodBaanCompleto+"'";
			updateStatement.addBatch(insertarDetalleLote);
			updateStatement.executeBatch();
			ConnRs.commit();
		} 
		Rsquery.close();
	} //
	
	
	if (parModalidad!=null && parModalidad.equals("modificar")) {
		query="SELECT anio, codigo_baan FROM lote WHERE anio = "+parAnio+" AND codigo_baan = '"+parCodBaan+"'";
		StatementRs=ConnRs.prepareStatement(query);
		Rsquery=StatementRs.executeQuery();
		Rsquery_hasData = Rsquery.next();
		if(Rsquery_hasData && (!parAnioAnt.equals(parAnio) || !parCodBaanAnt.equals(parCodBaan))) {
			out.print("<script>");
			out.print("alert('El Lote que desea insertar ya existe, por favor, inserte un Lote distinto');");
			out.print("history.go(-1);");
			out.print("</script>");
		} else {
			//-- actualiza el lote
			query = "UPDATE lote SET "+
					"anio = "+parAnio+", "+
					"codigo_baan ='"+parCodBaan+"', "+
					"descripcion = '"+parDescripcion+"', "+
					"nombre = '"+parNombre+"', "+
					"tipo_grano = "+parTipoGrano+" "+
					"WHERE id_lote = "+parId_lote;
			updateStatement = ConnRs.createStatement();
			updateStatement.addBatch(query);
			//Borrar detalle lote
			borrarDetalleLote = 
				"delete from Lote_Detalle where lote = '"+parId_lote+"'";
			if( parCodBaanCompleto != null && !parCodBaanCompleto.equals("") ){
				updateStatement.addBatch(borrarDetalleLote);
			}
			//Agregar detalle lote
			insertarDetalleLote = 
				"insert 	into Lote_Detalle ( "+
				"	lote,"+
				"	planta,"+
				"	linea,"+
				"	numerooc,"+
				"	numeroio) "+
				" select (select max(id_lote) from lote), "+
				"	Planta, "+
				"	Linea, "+
				"	NumeroOC,"+ 
				"	NumeroIO "+
				" from 	OCSap"+
				" where 	NumeroOC = '"+parCodBaanCompleto+"'";
			if( parCodBaanCompleto != null && !parCodBaanCompleto.equals("") ){
				updateStatement.addBatch(insertarDetalleLote);
			}
			updateStatement.executeBatch();
			ConnRs.commit();
		}
		Rsquery.close();
	
	} //if (parModalidad!=null && parModalidad.equals("modificar"))
	
}catch(Exception exception){
	error = "Error en las transacciones ("+exception.getMessage()+" )";
	ConnRs.rollback();
}finally{
	try{
		ConnRs.setAutoCommit(true);
		ConnRs.close();
	}catch(Exception exception){
		
	}
}
out.print("<script>");
if( error != null ){
	out.print("alert(\""+error+"\");");
}
out.print("window.opener.location.reload();");
out.print("window.close();");
out.print("</script>");
%>
<html>
<head>
<title>CIMA v2 - Guardar Lote</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="miestilo.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">
</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
